package com.g2room.io.grep;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class GrepCommon {
    public static String domain = "https://www.txt80.com";
    public static String dir = "E:/资料/TXT01/";

    public static Document getDoc(String url) {
        Document doc = null;
        try {
            doc = Jsoup.connect(getUrl(url))
                    .timeout(10000)
                    .ignoreContentType(true)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
                    .get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doc;
    }

    public static String getUrl(String url) {
        return url.startsWith("https://") ? url : domain + url;
    }

    public static void downloadNet(String uri, String name) throws MalformedURLException {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;
        URL url = new URL(getUrl(uri));
        URLConnection conn = null;
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            conn = url.openConnection();
            inStream = conn.getInputStream();
            fs = new FileOutputStream(dir + (null == name ? uri.substring(uri.lastIndexOf("/")) : name));
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fs) fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != inStream) inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
